﻿Namespace EIB.Helper
    Public Module HexConverters
        Public Function ConvertByteArrayToHexString(data As Byte()) As String
            Dim str As String = ""

            For Each b In data
                str = String.Format("{0} {1}", str, ConvertByteToHexString(b))
            Next

            Return str.Trim.ToUpper
        End Function

        Public Function ConvertByteToHexString(data As Byte) As String
            Dim hex As String = Convert.ToString(data, 16)

            While hex.Length < 2
                hex = String.Format("{0}{1}", "0", hex)
            End While

            Return hex
        End Function

        Public Function ConvertHexStringToByteArray(data As String) As Byte()
            Dim arr = data.Split(" ")
            Dim ba As Byte() = New Byte(arr.Length - 1) {}

            For i = 0 To arr.Length - 1
                ba(i) = ConvertHexStringToByte(arr(i))
            Next

            Return ba
        End Function

        Public Function ConvertHexStringToByte(data As String) As Byte
            Return Convert.ToByte(data, 16)
        End Function
    End Module
End Namespace